#include "b_config.h"
#include "ciu32l051_std.h"
#include "ciu32l051_std_adc.h"
#include "ciu32l051_std_rcc.h"
#include "hal/inc/b_hal_adc.h"

/* Private variables */
// static std_adc_config_t s_adc_config = {0};

/* Private function prototypes */
static void    adc_clock_config(void);
static uint8_t convert_channel(bAdcChannel_t channel);
static uint8_t convert_sample_time(bAdcSampleTime_t sample_time);
static uint8_t convert_resolution(bAdcResolution_t resolution);

int bMcuAdcInit(bAdcConfig_t *config)
{

    if (config == NULL)
        return -1;
    std_gpio_init_t adc_gpio_config = {0};
    uint32_t        adc_channel;
    std_rcc_gpio_clk_enable(RCC_PERIPH_CLK_GPIOA);
    if (config->channel == bADC_CHANNEL_6)
    {
        adc_gpio_config.pin  = GPIO_PIN_7;
        adc_gpio_config.mode = GPIO_MODE_ANALOG;
        std_gpio_init(GPIOA, &adc_gpio_config);
        adc_channel = ADC_CHANNEL_6;
    }
    else if (config->channel == bADC_CHANNEL_7)
    {
        adc_gpio_config.pin  = GPIO_PIN_0;
        adc_gpio_config.mode = GPIO_MODE_ANALOG;
        std_gpio_init(GPIOB, &adc_gpio_config);
        adc_channel = ADC_CHANNEL_7;
    }
    else
    {
        return -1;
    }
    // /* Configure ADC clock */
    adc_clock_config();
    std_adc_init_t adc_config = {0};
    /* Ê¹ÄÜADCÊ±ÖÓ */
    std_rcc_apb2_clk_enable(RCC_PERIPH_CLK_ADC);

    /* ADC³õÊ¼»¯ÅäÖÃ */
    adc_config.clock_src    = ADC_SYNC_CLK_PCLK_DIV1;    /* Í¬²½Ê±ÖÓPCLK£¬²»·ÖÆµ */
    adc_config.conver_mode  = ADC_SINGLE_CONVER_MODE;    /* µ¥´Î×ª»»Ä£Ê½ */
    adc_config.overrun_mode = ADC_OVRN_MODE_OVERWRITTEN; /* Êý¾ÝÒç³öÄ£Ê½: Êý¾Ý¸²¸Ç */
    adc_config.trigger_edge = ADC_TRIG_SW;               /* Èí¼þ´¥·¢ */
    adc_config.sampt1       = ADC_SAMPTIME_119CYCLES;    /* ²ÉÑùÊ±¼äÉèÖÃÎª119CLK£¬×éSAMPT1 */
    adc_config.sampt2       = ADC_SAMPTIME_119CYCLES;    /* ²ÉÑùÊ±¼äÉèÖÃÎª119CLK£¬×éSAMPT2 */
    adc_config.scan_dir     = ADC_SCAN_DIR_FORWARD;      /* ÕýÏòÉ¨Ãè */
    std_adc_init(&adc_config);

    std_adc_fix_sequence_channel_enable(adc_channel);
    std_adc_channel_sample_time_select(ADC_SAMPTIME_GROUP_2, adc_channel);
    std_adc_wait_mode_enable();

    /* ADC²Î¿¼µçÑ¹Ô´Ñ¡Ôñ */
    std_adc_set_reference_source(ADC_REFERENCE_VDDA);

    /* Ê¹ÄÜADC */
    std_adc_enable();

    /* µÈ´ýADCÊ¹ÄÜ×´Ì¬ÎÈ¶¨
       µ±ADC_CKÊ±ÖÓ´óÓÚµÈÓÚ6MHz£¬ÐèµÈ´ý2.5usÎÈ¶¨Ê±¼ä
       µ±ADC_CKÊ±ÖÓÐ¡ÓÚ6MHz£¬ÐèµÈ´ý17¸öADCÊ±ÖÓÖÜÆÚÎÈ¶¨Ê±¼ä
    */
    std_delayus(ADC_EN_DELAY);

    /* Ê¹ÄÜÐ£×¼ */
    std_adc_calibration_enable();

    /* µÈ´ýÐ£×¼Íê³É */
    while (!std_adc_get_flag(ADC_FLAG_EOCAL))
        ;

    return 0;
}

int bMcuAdcDeInit(void)
{
    /* Disable ADC */
    // std_adc_disable();

    // /* Reset ADC */
    // std_rcc_apb2_reset(RCC_PERIPH_RESET_ADC);
    // std_rcc_apb2_reset_release(RCC_PERIPH_RESET_ADC);

    return 0;
}

int bMcuAdcStart(void)
{
    /* Enable ADC */
    // if(std_adc_enable() != STD_OK)
    // {
    //     return -1;
    // }

    // /* Start ADC conversion */
    // if(std_adc_start_conversion() != STD_OK)
    // {
    //     return -1;
    // }

    return 0;
}

int bMcuAdcStop(void)
{
    /* Stop ADC conversion */
    // std_adc_stop_conversion();

    // /* Disable ADC */
    // std_adc_disable();

    return 0;
}
static uint32_t adc_sample_buf[2] = {0};
/*采样次数*/
#define ADC_CHANNEL_NUM (2)
int bMcuAdcRead(bAdcChannel_t channel, uint16_t *value)
{
    uint8_t  i                     = 0;
    uint32_t get_single_sample_sum = 0;
    uint32_t get_channel_voltage   = 0;

    /* 每次采样所有通道*/
    for (i = 0; i < ADC_CHANNEL_NUM; i++)
    {
        std_adc_start_conversion();

        while (std_adc_get_flag(ADC_FLAG_EOC) == 0)
            ;

        std_adc_clear_flag(ADC_FLAG_EOC);

        adc_sample_buf[i] = std_adc_get_conversion_value();
    }

    // get_single_sample =  get_single_sample_sum / ADC_SAMPLE_NUM;

    // et_channel_voltage = (uint32_t)((get_single_sample * 3311) / ADC_CONVER_SCALE);
    if (channel == bADC_CHANNEL_6)
    {
        *value = adc_sample_buf[0];
    }
    else if(channel == bADC_CHANNEL_7)
    {
        *value = adc_sample_buf[1];
    }
    return 0;
}

int bMcuAdcReadMulti(bAdcChannel_t *channels, uint16_t *values, uint8_t count)
{

    return 0;
}

/* Private functions */
static void adc_clock_config(void)
{
    /* Enable ADC clock */
    std_rcc_apb2_clk_enable(RCC_PERIPH_CLK_ADC);
}

static uint8_t convert_channel(bAdcChannel_t channel)
{
    // switch(channel)
    // {
    //     case ADC_CHANNEL_0: return ADC_CHANNEL_0;
    //     case ADC_CHANNEL_1: return ADC_CHANNEL_1;
    //     case ADC_CHANNEL_2: return ADC_CHANNEL_2;
    //     case ADC_CHANNEL_3: return ADC_CHANNEL_3;
    //     case ADC_CHANNEL_4: return ADC_CHANNEL_4;
    //     case ADC_CHANNEL_5: return ADC_CHANNEL_5;
    //     case ADC_CHANNEL_6: return ADC_CHANNEL_6;
    //     case ADC_CHANNEL_7: return ADC_CHANNEL_7;
    //     case ADC_CHANNEL_8: return ADC_CHANNEL_8;
    //     case ADC_CHANNEL_9: return ADC_CHANNEL_9;
    //     case ADC_CHANNEL_10: return ADC_CHANNEL_10;
    //     case ADC_CHANNEL_11: return ADC_CHANNEL_11;
    //     case ADC_CHANNEL_12: return ADC_CHANNEL_12;
    //     case ADC_CHANNEL_13: return ADC_CHANNEL_13;
    //     case ADC_CHANNEL_14: return ADC_CHANNEL_14;
    //     case ADC_CHANNEL_15: return ADC_CHANNEL_15;
    //     case ADC_CHANNEL_TEMP: return ADC_CHANNEL_TEMP;
    //     case ADC_CHANNEL_VREF: return ADC_CHANNEL_VREF;
    //     case ADC_CHANNEL_VBAT: return ADC_CHANNEL_VBAT;
    //     default: return ADC_CHANNEL_0;
    // }
}

static uint8_t convert_sample_time(bAdcSampleTime_t sample_time)
{
    // switch(sample_time)
    // {
    //     case ADC_SAMPLETIME_1_5: return ADC_SAMPLETIME_1_5;
    //     case ADC_SAMPLETIME_7_5: return ADC_SAMPLETIME_7_5;
    //     case ADC_SAMPLETIME_13_5: return ADC_SAMPLETIME_13_5;
    //     case ADC_SAMPLETIME_28_5: return ADC_SAMPLETIME_28_5;
    //     case ADC_SAMPLETIME_41_5: return ADC_SAMPLETIME_41_5;
    //     case ADC_SAMPLETIME_55_5: return ADC_SAMPLETIME_55_5;
    //     case ADC_SAMPLETIME_71_5: return ADC_SAMPLETIME_71_5;
    //     case ADC_SAMPLETIME_239_5: return ADC_SAMPLETIME_239_5;
    //     default: return ADC_SAMPLETIME_1_5;
    // }
}

static uint8_t convert_resolution(bAdcResolution_t resolution)
{
    // switch(resolution)
    // {
    //     case ADC_RESOLUTION_12B: return ADC_RESOLUTION_12B;
    //     case ADC_RESOLUTION_10B: return ADC_RESOLUTION_10B;
    //     case ADC_RESOLUTION_8B: return ADC_RESOLUTION_8B;
    //     case ADC_RESOLUTION_6B: return ADC_RESOLUTION_6B;
    //     default: return ADC_RESOLUTION_12B;
    // }
}